﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MODEL;
using BLL;
using System.Text;

public partial class Modify : System.Web.UI.Page
{
    protected StudentModel model = new StudentModel();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            StudentBLL bll = new StudentBLL();
            string sID = ViewState["Sid"].ToString();
            if (sID != string.Empty)
            {
                StudentModel model = bll.QueryByCondition(sID);
                model.SPwd = Request.Form["txtPwd"];
                model.SCnName= Request.Form["txtCnName"];
                model.SCID = Convert.ToInt32( Request.Form["selClasses"]);
                string gender = Request.Form["gender"];
                if(gender.Equals(0))
                {
                    model.SGender=false;
                }
                else
                {
                    model.SGender=true;
                }
                model.STel = Request.Form["txtTel"];
                if (bll.Update(model))
                {
                    Response.Redirect("Default.aspx?msg=修改成功");
                }
                else
                {
                    Response.Redirect("Default.aspx?msg=修改失败");
                }
            }
        }
        else
        {
            if (new WebHelper().IsLogin())
            {
                StudentBLL bll = new StudentBLL();
                string Sid = Request.QueryString["id"];
                int i = -1;
                if (Sid != string.Empty && int.TryParse(Sid, out i))
                {
                    model = bll.QueryByCondition(Sid);
                    ViewState["Sid"] = model.SID;
                    if (model == null)
                    {
                        Response.Redirect("Default.aspx?msg=查询错误");
                    }

                }
                else
                {
                    Response.Redirect("Default.aspx?msg=参数错误");
                }
            }
            else
            {
                Response.Write("登录超时，滚粗重新登录");
            }
        }
    }

    protected string GetGender(bool SGender, bool isMale)
    {
        return isMale ? (SGender ? "checked" : "") : (SGender ? "" : "checked");
    }

    protected string GetClass(int scid)
    {
        StringBuilder sb = new StringBuilder();
        List<ClassModel> list = new ClassesBLL().GetAllClass();
            foreach (ClassModel c in list)
            {
                if (c.CID == scid)
                {
                    sb.Append("<option value='"+c.CID+"' selected>" + c.CName + "</option>");
                }
                else
                {
                    sb.Append("<option value='"+c.CID+"'>" + c.CName + "</option>");
                }
            }
            return sb.ToString();
        }
}